package com.example.administrator.xmut.kcb;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.xmut.MainActivity;
import com.example.administrator.xmut.R;
import com.example.administrator.xmut.http.CourseBean;

public class ShowTimeTable extends Activity {
	public int today=getWeekOfDate(null);
	private ViewPager pager;
	private ArrayList<CourseBean> data;
	private RecyclerView recyclerView;
	private List<News> newsList;
	private RecyclerViewAdapter adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.e("dddd", String.valueOf(today));
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.timetable);
		initView();


		//RecyclerView
		//setContentView(R.layout.activity_main_car);

//		LinearLayoutManager layoutManager=new LinearLayoutManager(this);
//
//
//		recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
//
//		initPersonData();
//		adapter=new RecyclerViewAdapter(newsList,ShowTimeTable.this);
//
//		recyclerView.setHasFixedSize(true);
//		recyclerView.setLayoutManager(layoutManager);
//		recyclerView.setAdapter(adapter);

	}

	@SuppressWarnings("unchecked")
	private void initView() {
		pager = (ViewPager) findViewById(R.id.pager);
		ArrayList<CourseBean> course = new ArrayList<CourseBean>();
		SharedPreferences sharedPreferences = getSharedPreferences("test",
				MainActivity.MODE_PRIVATE);
		int Size = sharedPreferences.getInt("Size", 0);


		for (int i = 0; i <Size;i++ ) {
			String course_name= sharedPreferences.getString("course_name__"+i , null);
			String course_week = sharedPreferences.getString("course_week__" +i, null);
			String course_teacher = sharedPreferences.getString("course_teacher__"+i, null);
			String course_address= sharedPreferences.getString("course_address__" +i, null);
			CourseBean c = new CourseBean(course_name, course_address,
					course_teacher, course_week);
			course.add(c);
		}

		data=course;
		ArrayList<View> view_list = new ArrayList<View>();
		if(today==1){char[] date = new char[] { '一', '二', '三', '四', '五', '六', '日' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==2){char[] date = new char[] { '二', '三', '四', '五', '六', '日', '一' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==3){char[] date = new char[] {'三', '四', '五', '六', '日', '一', '二'};
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==4){char[] date = new char[] { '四', '五', '六', '日','一', '二', '三' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==5){char[] date = new char[] {'五', '六', '日','一', '二', '三', '四' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==6){char[] date = new char[] { '六', '日','一', '二', '三', '四', '五' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}
		if(today==0){char[] date = new char[] {'日', '一', '二', '三', '四', '五', '六' };
			for (char d : date) {
			view_list.add(getViewByDate(d));
		}}


		MyPagerAdapter pagerAdapter = new MyPagerAdapter(view_list);
		pager.setAdapter(pagerAdapter);


	}

	private View getViewByDate(char date) {

		ListView list = new ListView(this);
		ArrayList<CourseBean> list_data = new ArrayList<CourseBean>();
// 使用getString方法获得value，注意第2个参数是value的默认值
		for (int i = 0; i < data.size(); i++) {
			CourseBean course = data.get(i);
			if (course.getWeek() == date) {
				list_data.add(course);
			}
		}

		ListViewAdapter listViewAdapter = new ListViewAdapter(this, list_data);
		list.setAdapter(listViewAdapter);
		list.setOnItemClickListener(new ListViewItemClickListener());
		return list;
	}


	class ListViewItemClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> adapter, View view,
				int position, long arg3) {
			CourseBean bean = (CourseBean) adapter.getItemAtPosition(position);
			ShowTimeTable.this.showCouseDetails(bean);
		}
	}


	public void showCouseDetails(CourseBean bean) {
		Builder builder = new Builder(this);
		AlertDialog dialog = builder.create();
		dialog.show();
		dialog.setContentView(R.layout.details_layout);
		TextView textView = (TextView) dialog.findViewById(R.id.name);
		textView.setText(bean.getCourse_name());
		textView = (TextView) dialog.findViewById(R.id.teacher);
		textView.setText(bean.getCourse_teacher());
		textView = (TextView) dialog.findViewById(R.id.address);
		textView.setText(bean.getCourse_address());
		textView = (TextView) dialog.findViewById(R.id.week);
		textView.setText(bean.getCourse_week());
	}
	private void initPersonData() {
		newsList =new ArrayList<>();

		newsList.add(new News(getString(R.string.news_one_title),getString(R.string.news_one_desc),R.mipmap.news_one));
		newsList.add(new News(getString(R.string.news_two_title), getString(R.string.news_two_desc), R.mipmap.news_two));
		newsList.add(new News(getString(R.string.news_three_title),getString(R.string.news_three_desc),R.mipmap.news_three));
		newsList.add(new News(getString(R.string.news_four_title), getString(R.string.news_four_desc), R.mipmap.news_four));
	}


	public static int getWeekOfDate(Date date) {
		Calendar calendar = Calendar.getInstance();
		if(date != null){
			calendar.setTime(date);
		}
		int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0){
			w = 0;
		}
		return w;
	}
}
